import { ipcMain } from "electron";
import fs from "node:fs";
import child_process from "child_process";

(() => {
  const notepad__ = "C:\\Program Files\\Notepad--\\Notepad--.exe";
  const vscode = "C:\\Program Files\\Microsoft VS Code\\Code.exe";
  const notepad = "C:\\Windows\\notepad.exe";

  /**
   * 使用系统编辑器打开文件
   */
  ipcMain.on("shell.openFileViaEditor", (_, filePath: string, preferVsc?: boolean) => {
    if (preferVsc && fs.existsSync(vscode)) {
      child_process.spawn(vscode, [filePath], { detached: true });
      return;
    }
    if (fs.existsSync(notepad__)) {
      child_process.spawn(notepad__, [filePath], { detached: true });
      return;
    }
    if (fs.existsSync(vscode)) {
      child_process.spawn(vscode, [filePath], { detached: true });
      return;
    }
    child_process.spawn(notepad, [filePath], { detached: true });
  });
})();
